<?php
namespace App\Http\Controllers\Inquiry;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Http\Validate\Inquiry\store;
use App\Models\Inquiry\InquirySheet;
use App\Service\Customer\CustomerService;
use App\Service\Goods\GoodsService;
use App\Service\Inquiry\InquiryService;
use Illuminate\Http\Request;

class InquiryController extends Controller
{
    /**
     *  存储询价单
     */
    public function store(Request $request)
    {
        \DB::beginTransaction();
        try{
            store::validate($request);
            $goods_info = GoodsService::getGoodsIdInfo($request->goods_id);
            if(!$goods_info || !$goods_info['is_up']){
                throw new \Exception('商品不存在');
            }
            if($goods_info['stock'] < $request->numgber){
                throw new \Exception('库存不足');
            }
            $uid = $request->uid;
            if(!$uid){
                Result::msg(-1,'error','用户数据有误');
            }
            $user = CustomerService::findUser([
                'uid' => $uid
            ]);

            $inquirySheet = InquiryService::getLastInquirySheet($user->uid);
            if($inquirySheet && (time()-strtotime($inquirySheet->created_at))<10){
                throw new \Exception('询价太过频繁请稍后再试');
            }
            InquiryService::create($request,$goods_info,$user);
            \DB::commit();
            Result::msg('0','success',[]);
        }catch (\Exception $e){
            \DB::rollBack();
            Result::msg(-1,'error',$e->getMessage());
        }

    }

    /**
     *  询价单
     */
    public function index(Request $request)
    {
        $status = $request->input('status',0);
        $page = $request->input('page',1);
        $uid = $request->uid;
        if(!$uid){
            Result::msg(-1,'error','用户数据有误');
        }
        $inquirys = InquiryService::getInquirySheet($uid,$status);
        if($inquirys->isEmpty()){
            Result::msg(-1,'success','无数据');
        }else{
            Result::msg('0','success',$inquirys);
        }
    }

    /**
     *  取消询价单
     */
    public function cancle(Request $request)
    {
        try{
            $uid = $request->uid;
            if(!$uid){
                Result::msg(-1,'error','用户数据有误');
            }
            $inquiry_id = $request->input('inquiryId','');
            if(!$inquiry_id){
                Result::msg(-1,'error','未找到询价单 ');
            }
            $inquiry = InquiryService::oneInquirySheet($uid,$inquiry_id);
            if(!$inquiry){
                Result::msg(-1,'error','未找到询价单 ');
            }
            $inquiry->status = 4;
            $inquiry->save();
            Result::msg('0','success',[]);
        }catch (\Exception $e){
            Result::msg(-1,'error',$e->getMessage());
        }
    }

    /**
     *  下单询价单信息
     */
    public function detail(Request $request)
    {
        $id = $request->input('id','');
        if(!$id){
            Result::msg(-1,'error','不存在询价单');
        }
        $inquiry_sheet = InquirySheet::where('id',$id)->first();
        if(!$inquiry_sheet){
            Result::msg(-1,'error','不存在询价单');
        }
        if($inquiry_sheet->status != 1){
            Result::msg(-1,'error','询价单状态有误');
        }

        Result::msg(0,'success',$inquiry_sheet);
    }
}